<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
use think\Controller;
class Login extends Controller
{
    public function index()
    {
        return $this->fetch();
    }
    public function loginDo(){
        $username = input('post.username');
        $password = input('post.password');
        $verify_code = input('post.verify_code');

        if(!captcha_check($verify_code)){
            $this->error('验证码不正确');
        }
        $obj = db('admin');
        $admin = $obj->field('id,username,password,role_id')->where(array('username'=>$username, 'status'=>1))->find();
        if (!$admin) {
            $this->error('管理员不存在');
        }
        if ($admin['password'] != passwordEncode($password)) {
            $this->error('密码错误');
        }

        $obj->where(array('id'=>$admin['id']))->update(['last_time'=>time(), 'last_ip'=>request()->ip(),'login_num'=>['exp','login_num+1']]);
        session('adminInfo',['username'=>$admin['username'],'role'=>$admin['role_id'],'id'=>$admin['id']]);

        \org\Role::getmanagemenu(array('id'=>$admin['id'],'roleid'=>$admin['role_id']));

        $this->redirect('Index/index');
    }
    public function logout(){
        session('adminInfo',null);
        $this->redirect('Login/index');
    }
}